#include "cball.h"

#include <QDebug>

CBall::CBall(int type, QObject *parent) :
    QObject(parent)
{
    m_BallType = type;
    m_Checked = eCheckStatusFalse;
}


CBall::CBall(const CBall &ball) :
    QObject(ball.parent())
{
    m_BallType = ball.m_BallType;
    m_Checked = ball.m_Checked;
}

CBall& CBall::operator=(const CBall &ball)
{
    m_BallType = ball.m_BallType;
    m_Checked = ball.m_Checked;
    return *this;
}

// 获取小球的类型
int CBall::GetBallType()
{
    return m_BallType;
}


// 设定选中状态
void CBall::setCheckState(bool checked)
{
    if (checked)
    {
        m_Checked = eCheckStatusTrue;
    }
    else
    {
        m_Checked = eCheckStatusFalse;
    }
}

// 获取选中状态
bool CBall::getCheckState()
{
    if (eCheckStatusTrue == m_Checked)
    {
        return true;
    }
    else
    {
        return false;
    }
}

///
/// \brief CBall::drawBall
/// 绘制小球
///
/// \param painter
/// 绘图的句柄
///
/// \param Rect
/// 绘图的区域
///
///
void CBall::drawBall(QPainter &painter, QRectF Rect)
{
    painter.save();
    painter.setBrush(ColorIdxToColor(m_BallType));
    painter.setPen(Qt::NoPen);

    float Shrink = 0.9;
    QRectF drawRect = Rect;
    float rectSize = qMin(Rect.width(), Rect.height());
    drawRect.setX(Rect.x() + (Rect.width() - rectSize) / 2);
    drawRect.setY(Rect.y() + (Rect.height() - rectSize) / 2);
    drawRect.setWidth(rectSize);
    drawRect.setHeight(rectSize);

    if (m_Checked == eCheckStatusFalse)
    {// 如果没有选中
        m_flashStatus = 0;
    }
    else
    {
        switch (m_flashStatus)
        {
        case 0:
            break;
        case 1:
            Shrink = 0.9;
            break;
        case 2:
            Shrink = 0.8;
            break;
        case 3:
            Shrink = 0.7;
            break;
        default:
            Shrink = 1;
            break;
        }

    }

    drawRect.setX(drawRect.left() + drawRect.width() * (1 - Shrink) * 0.5);
    drawRect.setY(drawRect.top() + drawRect.height() * (1 - Shrink) * 0.5);
    drawRect.setWidth(drawRect.width() * Shrink);
    drawRect.setHeight(drawRect.height() * Shrink);

    m_flashStatus ++;
    m_flashStatus %= 4;

    painter.drawEllipse(drawRect);

    painter.restore();
}

// 通过小球的类型 设置小球的颜色
QColor CBall::ColorIdxToColor(int colorType)
{
    QColor ballColor;
    switch (colorType) {
    case eColorTypeGray:
        ballColor = QColor(Qt::gray);
        break;
    case eColorTypeRed:
        ballColor = QColor(Qt::red);
        break;
    case eColorTypeGreen:
        ballColor = QColor(Qt::green);
        break;
    case eColorTypeBlue:
        ballColor = QColor(Qt::blue);
        break;
    case eColorTypeCyan:
        ballColor = QColor(Qt::cyan);
        break;
    case eColorTypeMagenta:
        ballColor = QColor(Qt::magenta);
        break;
    case eColorTypeYellow:
        ballColor = QColor(Qt::yellow);
        break;
    default:
        ballColor = QColor(Qt::red);
        break;
    }

    return ballColor;
}
